package com.xbf.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbf.entity.Admin;
import com.xbf.entity.Goods;
import com.xbf.entity.Order;
import com.xbf.entity.User;
import com.xbf.mapper.AdminMapper;
import com.xbf.mapper.OrderMapper;
import com.xbf.mapper.UserMapper;
import com.xbf.service.AdminService;
import com.xbf.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Admin login(String username, String password) {
        return lambdaQuery()
                .eq(Admin::getAName, username)
                .eq(Admin::getAPassword, password)
                .one();
    }

    @Override
    public List<User> getAllUsers() {
        return userMapper.selectList(null);
    }

    @Override
    public List<Order> getAllOrders() {
        List<Order> orders = orderMapper.selectList(null);

        // 关联商品信息
        orders.forEach(order -> {
            Goods goods = goodsService.getById(order.getGId());
            if (goods != null) {
                order.setGName(goods.getGName());
                order.setGPrice(goods.getGPrice());
            }
        });

        return orders;
    }
}